package wire

import (
	"github.com/beego/beego/v2/server/web"
	beecontext "github.com/beego/beego/v2/server/web/context"
)

type WireManager struct{}

func NewWireManager() *WireManager {
	return &WireManager{}
}

func (wm *WireManager) RegisterCORSFilters() {
	web.InsertFilterChain("/*", func(next web.FilterFunc) web.FilterFunc {
		return func(ctx *beecontext.Context) {
			ctx.Output.Header("Access-Control-Allow-Origin", "*")
			ctx.Output.Header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
			ctx.Output.Header("Access-Control-Allow-Headers", "Content-Type, Authorization")

			// 处理预检请求
			if ctx.Input.Method() == "OPTIONS" {
				ctx.Output.Status = 204
				return
			}
			next(ctx)
		}
	})
}
